package com.morristaedt.mirror.modules.voice;

import android.app.Activity;
import android.os.Environment;
import android.util.Log;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;
import com.morristaedt.mirror.configuration.ConfigurationSettings;

/**
 * 合成语音模块
 */
public class TtsModule {


    private static final String TAG = "TtsModule";

    private Activity context;
    private SpeechSynthesizer mTts;
    private ConfigurationSettings settings;


    // 默认云端发音人
    public static String voicerCloud = "xiaoqiang";
    public static int voicerCloudIndex = 9;
    // 云端发音人列表
    public static String[] cloudVoicersEntries = {"安静，不要说话",
            "青年女声", "青年男声",  "普通话(女声)",
            "粤语(女声)", "台湾普通话(女声)","四川话(女声)", "东北话(女声)", "河南话",
            "湖南话", "陕西话(女声)",
            "童声(男声)", "童声(女声)"};
    public static String[] cloudVoicersValue = {"",
            "xiaoyan", "xiaoyu", "xiaoqi",
            "xiaomei", "xiaolin","xiaorong", "xiaoqian", "xiaokun",
            "xiaoqiang","vixying",
            "xiaoxin", "nannan"};

    public TtsModule(Activity context) {
        this.context = context;
        settings = new ConfigurationSettings(context);
        if (settings.getTtsVoice() == 0){
            return;
        }
        // 初始化合成对象
        mTts = SpeechSynthesizer.createSynthesizer(context, mTtsInitListener);
        setParam();
    }

    public void speak(String text,SynthesizerListener mTtsListener){
        int code = mTts.startSpeaking(text, mTtsListener);

        if (code != ErrorCode.SUCCESS) {
            Log.i(TAG, "语音合成失败,错误码: " + code);
        }
    }

    public void stopSpeak(){
        if (mTts != null){
            mTts.stopSpeaking();
        }
    }
    public void resumeSpeaking(){
        if (mTts != null){
            mTts.resumeSpeaking();
        }
    }
    public void pauseSpeaking(){
        if (mTts != null){
            mTts.pauseSpeaking();
        }
    }
    public void destroy(){
        if (mTts != null){
            mTts.destroy();
        }
    }

    private void setParam() {

        // 清空参数
        mTts.setParameter(SpeechConstant.PARAMS, null);
        //设置合成
        //设置使用云端引擎
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
        //设置发音人
        mTts.setParameter(SpeechConstant.VOICE_NAME, cloudVoicersValue[settings.getTtsVoice()]);
        //设置合成语速
        mTts.setParameter(SpeechConstant.SPEED, "50");
        //设置合成音调
        mTts.setParameter(SpeechConstant.PITCH, "50");
        //设置合成音量
        mTts.setParameter(SpeechConstant.VOLUME, "50");
        //设置播放器音频流类型
        mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");

        // 设置播放合成音频打断音乐播放，默认为true
        mTts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");

        // 设置音频保存路径，保存音频格式支持pcm、wav，设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
        // 注：AUDIO_FORMAT参数语记需要更新版本才能生效
        mTts.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, Environment.getExternalStorageDirectory() + "/msc/tts.wav");
    }

    /**
     * 初始化监听。
     */
    private InitListener mTtsInitListener = new InitListener() {
        @Override
        public void onInit(int code) {
            Log.d(TAG, "InitListener init() code = " + code);
            if (code != ErrorCode.SUCCESS) {
                Log.i(TAG, "初始化失败,错误码: " + code);

            } else {
                // 初始化成功，之后可以调用startSpeaking方法
                // 注：有的开发者在onCreate方法中创建完合成对象之后马上就调用startSpeaking进行合成，
                // 正确的做法是将onCreate中的startSpeaking调用移至这里
            }
        }
    };
}
